package org.example.controller;

import org.example.mapper.BookMarksMapper;
import org.example.pojo.BookMarks;
import org.example.pojo.Comment;
import org.example.pojo.Like;
import org.example.pojo.PageBean;
import org.example.service.BookMarksService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/bookmarks")
public class BookMarksController {
    @Autowired
    private BookMarksService bookMarksService;
    @Autowired
    private BookMarksMapper bookMarksMapper;
    @GetMapping("/list")
    public Result bookMarksList(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer size,
                                Integer userId,
                                String keywords,
                                Integer asterisk,
                                Integer groupId,
                                Integer tagId,
                                String begin,
                                String end
                                ) {
            PageBean pageBean = bookMarksService.bookMarksList(page, size, userId, keywords, asterisk, groupId, tagId, begin, end);
            return Result.success(pageBean);

    }
    @GetMapping("/detail")
    public Result bookMarkDetail(Integer id) {
            BookMarks bookMarks = bookMarksService.bookMarkDetail(id);
            return Result.success(bookMarks);
    }
    @GetMapping("/repeat")
    public Result repeatBookmarks() {
        List<BookMarks> bookMarksList = bookMarksMapper.bookMarksList(null, null, null, null, null, null, null);
        Integer total = bookMarksList.size();
        Map<String, Integer> urlCount = new HashMap<>();
        List<BookMarks> repeatBookMarks = new ArrayList<>();
        for (BookMarks bookMarks : bookMarksList) {
            String url = bookMarks.getUrl();
            urlCount.put(url, urlCount.getOrDefault(url, 0) + 1);
        }
        for (BookMarks bookMarks : bookMarksList) {
            String url = bookMarks.getUrl();
            if (urlCount.get(url) != 1) {
                repeatBookMarks.add(bookMarks);
                urlCount.put(url, urlCount.get(url) - 1);
            }
        }
        Integer repeatTotal = repeatBookMarks.size();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("repeatTotal", repeatTotal);
        data.put("repeatBookMarks", repeatBookMarks);
        return Result.success(data);
    }
    @PostMapping
    public Result addBookMarks(@RequestBody BookMarks bookMarks) {
            if (bookMarks.getGroupId() == null) {
                bookMarks.setGroupId(0);
            }
            bookMarksService.addBookMarks(bookMarks);
            return Result.success();
    }
    @DeleteMapping
    public Result deleteBookMarks(@RequestBody List<Integer> ids) {
            bookMarksService.delBookMarks(ids);
            return Result.success();
    }
    @PutMapping
    public Result updateBookMarks(@RequestBody BookMarks bookMarks) {
            bookMarksService.editBookMarks(bookMarks);
            return Result.success();
    }
    @PostMapping("/asterisk")
    public Result setAsterisk(@RequestBody Map<String, Object> data) {
        Integer asterisk = (Integer) data.get("asterisk");
        List<Integer> ids = (List<Integer>) data.get("ids");
        bookMarksMapper.setAsterisk(asterisk, ids);
        return Result.success();
    }
    @PostMapping("/comment")
    public Result addComment(@RequestBody Comment comment) {
            bookMarksService.addComment(comment);
            return Result.success();
    }
    @DeleteMapping("/comment")
    public Result delComment(@RequestBody Comment comment) {
            bookMarksService.delComment(comment);
            return Result.success();
    }
    @GetMapping("/comment/{bookmarkId}")
    public Result getComments(@PathVariable("bookmarkId") Integer bookmarkId) {
            List<Comment> comments = bookMarksService.getCommentsByBookmarkId(bookmarkId);
            return Result.success(comments);
    }
    @GetMapping("/like/{bookmarkId}")
    public Result getLikes(@PathVariable("bookmarkId") Integer bookmarkId) {
            Integer count = bookMarksService.getLikeCountByBookmarkId(bookmarkId);
            return Result.success(count);
    }
    @PostMapping("/likeStatus")
    public Result getLikeStatus(@RequestBody Like like) {
            Integer count = bookMarksService.getLikeStatus(like);
            if (count == 0) {
                return Result.success(false);
            } else {
                return Result.success(true);
            }
    }
    @PostMapping("/like")
    public Result addLike(@RequestBody Like like) {
            bookMarksService.addLike(like);
            return Result.success();
    }
    @DeleteMapping("/like")
    public Result cancelLike(@RequestBody Like like) {
            bookMarksService.cancelLike(like);
            return Result.success();
    }
    @GetMapping("/recommend/{menuId}")
    public Result recommendList(@PathVariable String menuId) {
        String url = "https://www.cangquyun.com/prod-api/browsing/categoryList?menuId=" + menuId;
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        String json = response.getBody();
        return Result.success("操作成功",json);
    }
}
